package com.uminho.calendar;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.Log;

import com.uminho.egendme.R;

	/****c* Calendar/Cell
  	 * DESCRIPTION
	 * Classe responsável pela auxilio no desenho das celulas do calendário
	 *
	 ****
	 */

public class Cell {
	private final int slots=24;
	HashMap<Date, Integer> busyDays=null;
	private Context context=null;
	public boolean actualMonth=true;
	/*private static String TAG = "Cell";*/
	protected Rect mBound = null;
	protected int mDayOfMonth = 1;	// from 1 to 31
	protected Paint mPaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG
            |Paint.ANTI_ALIAS_FLAG);
	int dx, dy;
	int thismonth, thisyear;
	boolean weekend = false;
	boolean isToday;
	
	
	public Cell(int dayOfMon, Rect rect, float textSize, boolean bold, Context cont, HashMap<Date, Integer> busy, int m, int y, boolean isToday) {
		this.context=cont;
		busyDays=busy;
		mDayOfMonth = dayOfMon;
		mBound = rect;
		mPaint.setTextSize(textSize/*26f*/);
		mPaint.setColor(Color.WHITE);
		if(bold) mPaint.setFakeBoldText(true);
		
		dx = (int) mPaint.measureText(String.valueOf(mDayOfMonth))/* / 2*/;
		dy = (int) (-mPaint.ascent() + mPaint.descent()) / 2;
		thismonth=m;
		thisyear=y;
		this.isToday=isToday;
	}
	
	public Cell(int dayOfMon, Rect rect, float textSize, Context context, HashMap<Date, Integer> busy, int m, int y, boolean isToday) {
		this(dayOfMon, rect, textSize, false, context, busy, m, y, isToday);
	}
	
	protected void draw(Canvas canvas) {
		GregorianCalendar aux = new GregorianCalendar(thisyear,thismonth,mDayOfMonth);
		
		Integer nocupied = busyDays.get(aux.getTime());
		//if (nocupied != null && actualMonth)Log.d("ocupied", ""+nocupied.toString());
		
		LayerDrawable a;
		Drawable day;
		
		if(nocupied==null)
			nocupied=0;
		float occupancy=(float)nocupied/slots;
		day = context.getResources().getDrawable(R.drawable.day);
		
		day.setBounds(mBound);
		if(actualMonth){
			//Current Month
			if(isToday){
				//Today
				day.setColorFilter( 0xffff0000, Mode.MULTIPLY);
				day.setAlpha(200);
			}
			else if(weekend){
				//Current Month's Weekend
				day.setAlpha(180);
			}
			else
				//Normal Current Month's Week
				day.setAlpha(110);
		}
		else{
			//Other Month
			day.setAlpha(60);
		}
		
		//Set margin
		int m = mBound.width()/9;
		//Set size of the dots
		int dotSize = m*2;
		
		//Number of dots that represent 100% of occupancy
		int totalDots = (mBound.height())/(m+dotSize);
		
		int numDots;
		if(nocupied==0 || !actualMonth)
			numDots=0;
		else{
			if(nocupied>=100){
				numDots=4;
			}
			else if(nocupied>=50){
				numDots=3;
			}else if(nocupied>=25){
				numDots=2;
			}else{
				numDots=1;
			}
			//numDots = (int)(occupancy*totalDots);
			//if(numDots==0) numDots=1; //Guarantee that at least one dot is drawn
		}
		Drawable[] layers = new Drawable[numDots+1];
		int left = mBound.left;
		int top = mBound.top;
		
		for(int i=0;i<numDots;i++){
			ShapeDrawable mDrawable = new ShapeDrawable(new OvalShape());
			mDrawable.getPaint().setColor(Color.WHITE);
			mDrawable.setBounds(left+m, top+m, left+m+dotSize, top+m+dotSize);
			layers[i+1] = mDrawable;
			top+=(m+dotSize);
		}
		
		layers[0] = day;
		
		a = new LayerDrawable(layers);
		a.draw(canvas);
		a.clearColorFilter();
		
		canvas.drawText(String.valueOf(mDayOfMonth), mBound.right-dx-m, mBound.bottom-m, mPaint);
	}
	
	public int getDayOfMonth() {
		return mDayOfMonth;
	}
	
	public boolean hitTest(int x, int y) {
		return mBound.contains(x, y); 
	}
	
	public Rect getBound() {
		return mBound;
	}
	
	@Override
	public String toString() {
		return String.valueOf(mDayOfMonth)+"("+mBound.toString()+")";
	}
	
}

